//
//  CartoonSetTableViewCell.m
//  Cartoon
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 YSZ. All rights reserved.
//

#import "CartoonSetTableViewCell.h"
#import "CartoonSet.h"

@implementation CartoonSetTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.myImageView = [[UIImageView alloc] init];
        self.myLabel = [[UILabel alloc] init];
        self.mySwitch = [[UISwitch alloc] init];
        NSString *dbPath = [NSString stringWithFormat:@"%@/Documents/1.rdb",NSHomeDirectory()];
        
        NSLog(@"%@",dbPath);
  
        BOOL result = [[FMDB shareFMDB].db open];
        
        if (result == NO) {
            NSLog(@"NO == %d",result);
            
        }else{
            
            BOOL creatResult = [[FMDB shareFMDB].db executeUpdate:[NSString stringWithFormat:@"create table cartoonset (myswitch bool primary key)"]];
            NSLog(@"创建是否成功 :%d",creatResult);
            
            FMResultSet *set = [[FMDB shareFMDB].db executeQuery:@"SELECT * FROM cartoonset"];
            
            
            if (!set.next) {
                BOOL swi = NO;
                BOOL insertResult = [[FMDB shareFMDB].db executeUpdate:[NSString stringWithFormat:@"insert into cartoonset values ('%d')",swi]];
                NSLog(@"插入结果:%d",insertResult);
            }else{
                _mySwitch.on = [set boolForColumn:@"myswitch"];
                NSLog(@"%d",_mySwitch.on);
            }
        }
            
        self.mySwitch.hidden = YES;
        [self.mySwitch addTarget:self action:@selector(switchActin:) forControlEvents:UIControlEventValueChanged];
        [self.contentView addSubview:_myImageView];
        [self.contentView addSubview:_myLabel];
        [self.contentView addSubview:_mySwitch];
    }
    return self;
}
- (void)switchActin:(UISwitch *)swi
{
    if (swi.isOn) {
        NSLog(@"加密");
        [self.delegate switchTouchID:self];
        [[FMDB shareFMDB].db executeUpdate:[NSString stringWithFormat:@"update cartoonset set myswitch = '%d' where myswitch = '%d'",swi.on,!swi.on]];
        
    }else{
        NSLog(@"不加密");
        [self.delegate switchTouchID:self];
        [[FMDB shareFMDB].db executeUpdate:[NSString stringWithFormat:@"update cartoonset set myswitch = '%d' where myswitch = '%d'",swi.on,!swi.on]];
    }
}

- (void)layoutSubviews
{
    self.myImageView.frame = CGRectMake(10, 10, self.bounds.size.height - 20, self.bounds.size.height - 20);
    self.myLabel.frame = CGRectMake(self.bounds.size.height, self.bounds.size.height / 2 - 20, self.bounds.size.width - self.bounds.size.height, 40);
    self.mySwitch.frame = CGRectMake(self.bounds.size.width - 60, self.bounds.size.height / 2 - 15, 50, 20);
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
